package com.firexxx.vp.domain.stb;

import com.firexxx.common.domain.AbstractEntity;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @description: TODO
 * @throws:
 * @author: zhuangzhang2
 * @date: 2019/12/18 19:40
 */
@Entity
@Table(name = "stb")
@DynamicInsert
@DynamicUpdate
@Where(clause = "is_deleted = false")
@Getter
@NoArgsConstructor(access = AccessLevel.PACKAGE)
@Slf4j
public class StbEntity extends AbstractEntity {

    @NotNull
    @Embedded
    @Setter
    private StbKey stbKey;

    //user_id即vp_id,ElementCollection级联对象会随着StbEntity一起加载
    @Setter
    @ElementCollection
    @CollectionTable(name = "stb_user_id", joinColumns = @JoinColumn(name = "stb_id"))
    @Column(name = "user_id")
    private List<String> userIdList;

    @Setter
    @Column(name = "user_group_id")
    private String userGroupId;

    public StbEntity(String channelId, String stbId, String mac) {
        this.stbKey = new StbKey(channelId, stbId, mac);
    }

    public String updateGroup(String newGroupId, List<String> vpIdList) {
        String oldGroupId = userGroupId;
        userGroupId = newGroupId;
        this.userIdList = vpIdList;

        log.info("stb{}分组从{}[{}]切换到{}[{}]", stbKey, oldGroupId, updateDateTime,
                newGroupId, new java.util.Date());

        return oldGroupId;
    }
}